<div class="row">
	<div class="col-xs-12">
		<div class="box">
			<div class="box-header">
				<h3 class="box-title">菜单管理</h3>

				<div class="box-tools">
					<div class="input-group input-group-sm" style="width: 100px;">
						<div class="input-group-btn">
						
							<button type="button" 
							class="btn btn-default btn-delete">删除</button>
							<button type="button" class="btn btn-default btn-add">添加</button>
							<button type="button" class="btn btn-default btn-update">修改</button>
						</div>
					</div>
				</div>
			</div>
			<!-- /.box-header -->
		<div class="box-body table-responsive no-padding">
          <table id="menuTable" class="table table-hover">
        	<thead>
	        <tr>
		      <th data-field="selectItem" data-checkbox="true"></th>
	        </tr>
	     </thead>
         </table>
        </div>
		</div>
		<!-- /.box -->
	</div>
</div>
<script type="text/javascript" src="bower_components/treegrid/jquery.treegrid.extension.js"></script>
<script type="text/javascript" src="bower_components/treegrid/jquery.treegrid.min.js"></script>
<script type="text/javascript" src="bower_components/treegrid/tree.table.js"></script>
<script type="text/javascript">
/**
 * 初始化表格的列
 */
var columns = [
{
	field : 'selectItem',
	radio : true
},
{
	title : '菜单ID',
	field : 'id',
	visible : false,
	align : 'center',
	valign : 'middle',
	width : '80px'
},
{
	title : '菜单名称',
	field : 'name',
	align : 'center',
	valign : 'middle',
	sortable : true,
	width : '130px'
},
{
	title : '上级菜单',
	field : 'parentName',
	align : 'center',
	valign : 'middle',
	sortable : true,
	width : '100px'
},
{
	title : '类型',
	field : 'type',
	align : 'center',
	valign : 'middle',
	sortable : true,
	width : '70px',
	formatter : function(item, index) {
		if (item.type == 1) {
			return '<span class="label label-success">菜单</span>';
		}
		if (item.type == 2) {
			return '<span class="label label-warning">按钮</span>';
		}
	}
}, 
{
	title : '排序号',
	field : 'sort',
	align : 'center',
	valign : 'middle',
	sortable : true,
	width : '70px'
}, 
{
	title : '菜单URL',
	field : 'url',
	align : 'center',
	valign : 'middle',
	sortable : true,
	width : '160px'
}, 
{
	title : '授权标识',
	field : 'permission',
	align : 'center',
	valign : 'middle',
	sortable : true
} ];

$(function(){
	doGetObjects();
	$(".input-group-btn")
	.on("click",'.btn-delete',doDeleteObject)
	.on("click",'.btn-add,.btn-update',doMenuEditUI)
});

function doMenuEditUI(){
	var title;
	if($(this).hasClass("btn-add")){
		title="添加菜单";
		$("#mainContentId").removeData();
		var url="menu/doMenuSaveEditUI.do";
		$.ajax({url:url,success:function(result){
			console.log(result);
				if(result.state==0){
					alert(result.message);
				}else{
					doLoadtitle(title,url);
				}
			}
		})
	}else{
		title="修改菜单";
		var rowData=getCheckedItem();
		if(!rowData){
			alert("请先选择一个修改项");
			return;
		}
		$("#mainContentId").data("rowData",rowData);
		var url="menu/doMenuUpdateEditUI.do";
		$.ajax({url:url,success:function(result){
			console.log(result);
				if(result.state==0){
					alert(result.message);
				}else{
					doLoadtitle(title,url);
				}
			}
		})
	}
}
function doLoadtitle(title,url){
	$("#mainContentId").load(url,function(){
		$(".box-title").html(title);
	});
}
function getCheckedItem(){
	var tr=$("tbody input[type='radio']:checked").parents("tr");
	console.log(tr.data("rowData"));
	return tr.data("rowData");
}

function doDeleteObject(){
	var id=doGetCheckedId();
	if(!id){
		alert("请先选一个")
		return;
	}
	var url="menu/doDeleteObject.do";
	var params={"id":id}
	console.log(params);
	$.post(url,params,function(result){
		if(result.state==1){
			alert(result.message);
			doGetObjects();
		}else{
			alert(result.message);
		}
	})
}

function doGetCheckedId(){
	var nodes=$("#menuTable").bootstrapTreeTable("getSelections");
	console.log(nodes);
	if(nodes.length==1)
		return nodes[0].id;
}

function doGetObjects(){
	//1.构建TreeTabel对象
	var treeTable = new TreeTable("menuTable","menu/doFindObjects.do",columns);
	treeTable.setExpandColumn(2);
	treeTable.init();
}

</script>
